<?php

class Promotion_project_model extends CI_Model
{
    private $table = 'promotion_project';

    public function findById($id)
    {
        $this->db->where('id', $id);
        return $this->db->get($this->table);
    }

    public function total($search_param = array())
    {
        if ( !empty($search_param['name']) ) {
            $this->db->where('name', $search_param['name']);
        }
        if ( !empty($search_param['url']) ) {
            $this->db->like('url', $search_param['url']);
        }
        if ( isset($search_param['belong']) && $search_param['belong'] != '-1' ) {
            $this->db->where('belong', $search_param['belong']);
        }
        if ( isset($search_param['version']) && $search_param['version'] != '-1' ) {
            $this->db->where('version', $search_param['version']);
        }
        return $this->db->count_all_results($this->table);
    }

    public function page_list($page_num, $num, $search_param = array())
    {
        if ( !empty($search_param['name']) ) {
            $this->db->where('name', $search_param['name']);
        }
        if ( !empty($search_param['url']) ) {
            $this->db->like('url', $search_param['url']);
        }
        if ( isset($search_param['belong']) && $search_param['belong'] != '-1' ) {
            $this->db->where('belong', $search_param['belong']);
        }
        if ( isset($search_param['version']) && $search_param['version'] != '-1' ) {
            $this->db->where('version', $search_param['version']);
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);

        return $this->db->get($this->table);
    }

    public function insertPromotionProject($postData, $imageData)
    {
        $data = array(
            'name'    => trim($postData['name']),
            'url'     => $postData['url'],
            'belong'  => $postData['belong'],
            'version' => $postData['version'],
        );
        if ( !empty($imageData['file_name']) ) {
            $data['images'] = $imageData['file_name'];
        }
        if ( !empty($postData['praise']) ) {
            $data['praise'] = $postData['praise'];
        }
        if ( !empty($postData['sort']) ) {
            $data['sort'] = $postData['sort'];
        }
        if ( !empty($postData['price']) ) {
            $data['price'] = $postData['price'];
        }
        if ( !empty($postData['sub_name']) ) {
            $data['sub_name'] = $postData['sub_name'];
        } else {
            $data['sub_name'] = trim($postData['name']);
        }
        if ( !empty($postData['item_id']) ) {
            $data['item_id'] = $postData['item_id'];
        }
        $this->db->insert($this->table, $data);

        return $this->db->insert_id();
    }

    public function updatePromotionProject($postData, $imageData)
    {
        $data = array(
            'name'       => trim($postData['name']),
            'url'        => $postData['url'],
            'belong'     => $postData['belong'],
            'version'    => $postData['version'],
            'created_at' => date('Y-m-d H:i:s', time()),
        );
        if ( !empty($imageData['file_name']) ) {
            $data['images'] = $imageData['file_name'];
        }
        if ( !empty($postData['praise']) ) {
            $data['praise'] = $postData['praise'];
        }
        if ( !empty($postData['sort']) ) {
            $data['sort'] = $postData['sort'];
        }
        if ( !empty($postData['price']) ) {
            $data['price'] = $postData['price'];
        }
        if ( !empty($postData['sub_name']) ) {
            $data['sub_name'] = $postData['sub_name'];
        } else {
            $data['sub_name'] = trim($postData['name']);
        }
        if ( !empty($postData['item_id']) ) {
            $data['item_id'] = $postData['item_id'];
        }
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }

    public function delete($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }
}